﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSite.Core;
using Common;
using System.Configuration;

namespace WebSite
{
    public partial class Reg : BasePage
    {
        public override bool NeedLogin
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            NavHelper.SetNavStepCurrentStep(1);
        }

        private bool checkaccount(string str)
        {
            const string allowchar = "abcdefghijklmnopqrstuvwxyz_1234567890";
            const string check_only_num = "1234567890";
            const string check_only_line = "_";

            List<char> listAllow = new List<char>(allowchar.ToCharArray());
            List<char> listOnlyNum = new List<char>(check_only_num.ToCharArray());
            List<char> listOnlyLine = new List<char>(check_only_line.ToCharArray());

            if (str.Length<4 || str.Length>16)
            {
                return false;
            }

            bool onlyNum=true;
            bool onlyLine=true;

            foreach (char c in str)
            {
                if (!listAllow.Contains(c))
                {
                    return false;
                }

                if (!listOnlyNum.Contains(c))
                {
                    onlyNum = false;
                }

                if (!listOnlyLine.Contains(c))
                {
                    onlyLine = false;
                }
            }

            if (onlyNum || onlyLine)
            {
                return false;
            }

            return true;
        }

        private bool checkpassword(string str)
        {
            if (str.Length<6 || str.Length>16)
            {
                return false;
            }

            return true;
        }

        protected void ibReg_Click(object sender, ImageClickEventArgs e)
        {
            if (tbUserName.Value.Trim() == "" || !checkaccount(tbUserName.Value))
            {
                tbUserName.Focus();
                AlertMessageBack("请填写有效的用户名");
            }
            if (tbPassword.Value.Trim() == "" || !checkpassword(tbPassword.Value) )
            {
                tbPassword.Focus();
                AlertMessageBack("请填写有效的密码");
            }
            //if (tbRepassword.Value.Trim() == "")
            //{
            //    tbRepassword.Focus();
            //    AlertMessageBack("请填写密码");
            //}
            if (tbPassword.Value != tbRepassword.Value)
            {
                tbRepassword.Focus();
                AlertMessageBack( "请按提示格式填写密码" );
            }
            if (tbMobile.Value.Trim() == "" || tbMobile.Value.Trim().Length != 11)
            {
                tbMobile.Focus();
                AlertMessageBack("请填写手机号码");
            }

            // context.Session["reg_phone"] = p.ToString();
                // context.Session["reg_code"] = codestr;

            if (ConfigurationManager.AppSettings["EnablePhoneCheckOnReg"] == "1")
            {
                if (tbMobile.Value != Session["reg_phone"] as string || tbMobileCheckCode.Value != Session["reg_code"] as string)
                {
                    AlertMessageBack( "验证码错误，请重新获取" );
                }
            }

            // TODO：check mobile check code
            Maticsoft.BLL.CarrefourCover.Account bll = new Maticsoft.BLL.CarrefourCover.Account();
            var list = bll.GetModelList("Username = '" + tbUserName.Value.Trim().SqlFilter() + "'");
            if (list.Count > 0)
            {
                tbUserName.Focus();
                AlertMessageBack("用户名重复，请修改");
            }

            var listcheckmob = bll.GetModelList( "telephone = '" + tbMobile.Value.Trim().SqlFilter() + "'" );
            if (listcheckmob.Count >= 10)
            {
                tbUserName.Focus();
                AlertMessageBack( "对不起，您的手机号已注册多次，请使用其他号码注册" );
            }

            int result = bll.Add(new Maticsoft.Model.CarrefourCover.Account()
            {
                UserName = tbUserName.Value.Trim(),
                Passwrod = this.tbPassword.Value.Trim(),
                Ip = Request.UserHostAddress,
                CreateTime = DateTime.Now,
                TelePhone = this.tbMobile.Value.Trim()
            });

            Project_Session.CurrentUser = new LoginUser()
            {
                UserId = result,
                UserName = tbUserName.Value.Trim()
            };

            if (string.IsNullOrEmpty(Request["ReturnUrl"]))
            {
                AlertMessage("注册成功", "/SubmitInfo.aspx");
            }
            else
            {
                AlertMessage("注册成功", Request["ReturnUrl"]);
            }            
        }


        protected void ibLogin_Click(object sender, ImageClickEventArgs e)
        {
            LoginHelper.SetCurrentUser(tbUserNameForLogin.Value.Trim(), tbPasswordForLogin.Value.Trim());
        }

    }
}